﻿/*
 * @author: wizardc
 */

using System;
using System.Diagnostics;
using UnityEditor;

namespace DouEditor.Utils
{
    /// <summary>
    /// 清理编辑器内存的工具
    /// </summary>
    public static class EditorGCUtil
    {
        [System.Runtime.InteropServices.DllImport("psapi.dll")]
        static extern int EmptyWorkingSet(IntPtr hwProc);
        
        [MenuItem("DouFramework/扩展/清理内存")]
        static void EditorGC()
        {
            GarbageCollect();
            EditorUtility.DisplayDialog("提示", "内存清理完毕！", "确定");
        }
        
        /// <summary>
        /// 清理编辑器内存
        /// </summary>
        public static void GarbageCollect()
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
            try
            {
                var process = Process.GetCurrentProcess();
                EmptyWorkingSet(process.Handle);
            }
            catch { }
        }
    }
}
